$(function () {
    $("#backPage").load("/html/backPage.html");
    getSexByCode();
})
// 查询所有用户
function findAll() {
    let data = {
        key: $("#selectKey").val(),
        startTime: $("#startTime").val(),
        endTime: $("#endTime").val(),
        roleId: $.trim($("#selectRole").val()),
        education: $.trim($("#selectEducation").val()),
        stateCode: $.trim($("#selectState").val()),
        pageNum: $("#pageNum").val(),
        pageSize: $("#pageSize").val()
    };
    console.log(data)
    let result = jsonGetValue("/user/getUserList", data);
    console.log(result);
    setAllData(result.data.list);
    $("#total").html(result.data.count);
}
// 给用户列表赋值
function setAllData(data) {
    let html = "";
    for (let i=0; i<data.length;i++) {
        html += "<tr>\n" +
            "        <td>"+ data[i].id +"</td>\n" +
            "        <td>"+ data[i].userCode +"</td>\n" +
            "        <td>"+ (data[i].name == undefined?"暂无":data[i].name)  +"</td>\n" +
            "        <td>"+ (data[i].sexName == undefined?"暂无":data[i].sexName) +"</td>\n" +
            "        <td>"+ (data[i].educationName == undefined?"暂无":data[i].educationName) +"</td>\n" +
            "        <td>"+ (data[i].roleName == undefined?"暂无":data[i].roleName) +"</td>\n" +
            "        <td>"+ (data[i].address == undefined?"暂无":data[i].address) +"</td>\n" +
            "        <td>"+ (data[i].phone == undefined?"暂无":data[i].phone) +"</td>\n" +
            "        <td>"+ data[i].createTime +'</td>\n';
        if (data[i].stateCode == "normal") {
            html += "<td class='yes'><div onclick=\"changeShowState('" + data[i].userCode + "','frozen')\"><span>"+ data[i].userState +"</span><span class='iconfont'>&#xe60d</span></div></td>\n";
        } else if (data[i].stateCode == "frozen") {
            html += "<td class='no'><div onclick=\"changeShowState('" + data[i].userCode + "','normal')\"><span>"+ data[i].userState +"</span><span class='iconfont'>&#xe600</span></div></td>\n";
        }
        html += "    <td>\n" +
            "            <div onclick=\"edit('"+ data[i].userCode +"')\">" +
            '            <a href="#edit-modal-box" style="text-decoration:none;">' +
            '            <i class="iconfont icon-edit">&#xe608;</i></a></div>\n' +
            "            <div onclick=\"del('"+ data[i].userCode +"')\">" +
            '            <a href="#del-modal-box" style="text-decoration:none;">' +
            '            <i class="iconfont icon-del">&#xe60b;</i></a></div>\n' +
            "        </td>\n" +
            "    </tr>";
    }

    $("#tbody").html(html)
    $("#tbody > tr > td").css({"vertical-align":"middle"})
    $("#tbody > tr > td:nth-child(10) > div").css({"cursor": "pointer","display": "flex","align-items": "center"})
    $("#tbody > tr > td:last-child").css({"display": "flex","justify-content": "flex-start"})
    $("#tbody > tr > td:last-child > div").css({"cursor": "pointer"})
    $("#tbody > tr > td:last-child > div").css({"margin-right":"10px"})
}

// 修改用户状态
function changeShowState(userCode,stateCode){
    let data = {
        userCode: userCode,
        stateCode:stateCode
    };
    let result = jsonPost("/user/updateState", data);
    console.log(result);
    if (result.data.data == 1) {
        page('go');
    } else {
        alert("修改状态失败");
    }
}

// 从码表获取性别
function getSexByCode() {
    let result = jsonGetValue("/codeTable/getCodeByType",{type:"sex"});
    console.log(result);
    setSexDataByCode(result.data.data);
}
// 给性别单选按钮赋值
function setSexDataByCode(data) {
    let html = "";
    for (let i=0;i<data.length;i++){
        html += '<label><input type="radio" name="userSexCode" value="'+ data[i].code +'">'+ data[i].key +'</label>';
    }
    $("#updateSex").html(html);
    getEducationByCode();
}

// 从码表获取学历
function getEducationByCode() {
    let result = jsonGetValue("/codeTable/getCodeByType",{type:"education"});
    console.log(result);
    setEducationDataByCode(result.data.data);
}
// 给学历下拉列表赋值
function setEducationDataByCode(data) {
    let html = "<option value='all'>全部</option>";
    for (let i=0;i<data.length;i++){
        html += '<div><option value ="'+ data[i].code +'">'+ data[i].key +'</option></div>';
    }
    $("#selectEducation").html(html);
    $("#updateEducation").html(html);
    getStateByCode();
}

// 从码表获取状态
function getStateByCode() {
    let result = jsonGetValue("/codeTable/getCodeByType",{type:"user_state"});
    console.log(result);
    setStateDataByCode(result.data.data);
}
// 给状态下拉列表赋值
function setStateDataByCode(data) {
    let html = "<option value='all'>全部</option>";
    for (let i=0;i<data.length;i++){
        html += '<div><option value ="'+ data[i].code +'">'+ data[i].key +'</option></div>';
    }
    $("#selectState").html(html);
    getRoleList();
}

// 获取角色
function getRoleList() {
    let result = jsonGet("/role/getRoleList");
    console.log(result);
    setRoleListData(result.data.data);
}
// 给角色下拉列表赋值
function setRoleListData(data) {
    let html = "<option value='all'>全部</option>";
    for (let i=0;i<data.length;i++){
        html += '<div><option value ="'+ data[i].id +'">'+ data[i].name +'</option></div>';
    }
    $("#selectRole").html(html);
    $("#updateRole").html(html);
}

// 根据用户编号查询用户
function findByUserCode(userCode) {
    let data = {userCode: userCode}
    let result = jsonGetValue("/user/findUserByUserCode", data);
    console.log(result);
    setUserDataByUserCode(result.data.data);
}
// 给修改和删除模态框赋值
function setUserDataByUserCode(data) {
    $("#userCode").val(data.userCode);
    $("#updateName").val(data.name);
    $("input[name='userSexCode'][value='"+ data.sexCode +"']").attr("checked",true);
    $("#updateEducation").find("option[value='"+ data.education +"']").prop("selected",true);
    $("#updateAddress").val(data.address);
    $("#updatePhone").val(data.phone);
    $("#updateRole").find("option[value='"+ data.roleId +"']").prop("selected",true);
    $("#delUserCode").val(data.userCode);
    $("#tips").html("是否删除用户:" + data.name +"?");
}

function edit(userCode) {
    findByUserCode(userCode);
}

// 修改信息
function doEdit() {
    let data = {
        userCode:$.trim($("#userCode").val()),
        name:$.trim($("#updateName").val()),
        sexCode:$.trim($("input[name='userSexCode']:checked").val()),
        education:$.trim($("#updateEducation").val()),
        address:$.trim($("#updateAddress").val()),
        phone:$.trim($("#updatePhone").val()),
        roleId:$.trim($("#updateRole").val())
    }
    let result = jsonPostParam("/user/updateUser", data);
    console.log(result)
    if (result.data.data == 1){
        $(".right").load("/html/user/user-list.html");
    }
}

// 弹出修改模态框
function del(userCode) {
    findByUserCode(userCode);
}
// 执行删除
function doDel() {
    let data = {userCode:$.trim($("#delUserCode").val())}
    let result = jsonPost("/user/delUser", data);
    console.log(result);
    if (result.data.data == 1) {
        $(".right").load("/html/user/user-list.html");
    }
}


// 查询点击事件
function select() {
    page("home");
}
// 清空点击事件
function clearAll() {
    $("input[ type='text']").val("");
    $("input[ type='date']").val("");
    $("#selectRole ").val("all");
    $("#selectEducation").val("all");
    $("#selectState").val("all");
    $("#pageSize").val(5);
    $("#pageNum").val(1);
}